# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


class WeiboCommentPipeline(object):
    def open_spider(self, spider):
        """
        爬虫开启时会执行此方法
        :param spider:
        :return:
        """
        self.file = open('comment.csv', 'w', encoding='gbk')

    def process_item(self, item, spider):
        try:
            res = dict(item)
            # 为了防止评论中有英文逗号导致csv分列错误，将字符串用英文双引号包裹可以解决这种问题
            line = '"%s","%s"' % (res['nick'], res['comment'])
            self.file.write(line + '\n')
        except:
            pass
        return item

    def close_spider(self, spider):
        """
        爬虫一旦关闭，就会执行此方法，关闭文件流
        :param spider:
        :return:
        """
        self.file.close()
